package pages;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import sun.rmi.runtime.Log;
import utils.Log4Test;
import utils.WebDriverWrapper;

/**
 * Created by Vadim on 6/10/14.
 */
public class SearchProduct extends BasePage{
    private static final String URL = "http://rozetka.com.ua/";
    protected final By searchString = By.className("header-search-input-text");
    protected final String productFoundLocator = "//a[contains(text(), '%s' )]";

    public SearchProduct(WebDriverWrapper driver) {
        super(driver,URL);
    }

    public void open(){
        driver.get(URL);
    };


    public boolean searchProduct(String productName) {
        Log4Test.info("searching product " + productName);
        try {
            driver.findElement(searchString).clear();
            driver.findElement(searchString).sendKeys(productName);
            driver.findElement(searchString).sendKeys(Keys.RETURN);
            driver.findElement(By.partialLinkText(productName)).click();
            return driver.findElements(By.xpath(String.format(productFoundLocator, productName))).size() > 0;
        }catch (org.openqa.selenium.TimeoutException e) {
            Log4Test.warn("After timeout product not found");
        }
        return false;
    }
}
